package cn.orange.ch01_array.binarysearch;

/**
 * LC367.有效的完全平方数
 给你一个正整数 num 。如果 num 是一个完全平方数，则返回 true ，否则返回 false
 */
public class LC367 {
    public boolean isPerfectSquare(int num) {
        int i = 1;
        int j = num;
        while (i <= j) {
            int mid = i + (j - i) / 2;
            long sqt = (long) mid * mid;
            if (sqt > num) {
                j = mid - 1;
            } else if (sqt < num) {
                i = mid + 1;
            } else {
                return true;
            }
        }
        return false;
    }
    public static void main(String[] args) {
        LC367 alg = new LC367();
        System.out.println(alg.isPerfectSquare(16));
        System.out.println(alg.isPerfectSquare(18));
    }
}
